package cn.mxz.controller;

import cn.hutool.extra.mail.MailUtil;
import cn.mxz.pojo.ResultData;
import cn.mxz.pojo.Users;
import cn.mxz.service.impl.TbUserServiceImpl;
import cn.mxz.utils.PhoneUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.security.util.Password;

/**
 * 作者；朱洪旭
 * 时间；2021/12/8 9:32
 */
@CrossOrigin
@RestController
@RequestMapping("/finduser")
public class FindUserController {
    @Autowired
     private TbUserServiceImpl tbUserService;
    String code=null;
    @GetMapping("/email")
    public ResultData emailverification(String username){
        if(username==null||username==""){return  new ResultData(121,"请填写用户名");}
        String s = randomStr(6);
        this.code=s;
        ResultData finduser = tbUserService.finduser(username);
        if(finduser.getCode()!=0){return  finduser;}
        Users users= (Users) finduser.getData();
        try{
            MailUtil.send(users.getUserEmail(),"你的验证码是",s,false);
        }catch (Exception e){
            e.printStackTrace();
            return new ResultData(104,"该账户没有绑定邮箱!");
        }
        return new ResultData(0,"发送验证码成功！");
    }
    @GetMapping("/checke")
    public  ResultData checkcode(String code,String username){
        if(code==null||code==""){return new ResultData(112,"验证码不能为空");}
        if(this.code.equalsIgnoreCase(code)){
            ResultData finduser = tbUserService.finduser(username);
            Users users= (Users) finduser.getData();
            String Password=users.getPassword();
            MailUtil.send(users.getUserEmail(),"你的密码是",Password,false);
            return new ResultData(0,"查询成功",Password);
        }
        return  new ResultData(111,"验证码不正确");
    }
    @GetMapping("/phone")
    public ResultData phoneverification(String username){
        if(username==""||username==null){return new ResultData(115,"用户名不能为空");}
        String s = randomStr(4);
        this.code=s;
        ResultData finduser = tbUserService.finduser(username);
        if(finduser.getCode()!=0){return  finduser;}
        Users users= (Users) finduser.getData();
        PhoneUtils.phonecode(users.getUserMobile(),s);
        return new ResultData(0,"发送电话短信");
    }
    @GetMapping("/checkphone")
    public  ResultData checkphone(String code,String username){
        if(code==null||code==""){return new ResultData(112,"验证码不能为空");}
        if(this.code.equalsIgnoreCase(code)){
            ResultData finduser = tbUserService.finduser(username);
            Users users= (Users) finduser.getData();
            String Password=users.getPassword();
            PhoneUtils.phonecode(users.getUserMobile(),Password);
            return new ResultData(0,"查询成功",Password);
        }
        return  new ResultData(111,"验证码不正确");
    }
    @GetMapping("/usermsg")
    public  ResultData selectusers(Integer id){
        ResultData usermsg = tbUserService.usermsg(id);
        return  usermsg;
    }
    private String randomStr(int n) {
        String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        String str2 = "";
        int len = str1.length() - 1;
        double r;
        for (int i = 0; i < n; i++) {
            r = (Math.random()) * len;
            str2 = str2 + str1.charAt((int) r);
        }
        return str2;
    }
}
